package com.huaqin.punan.autoanswer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;

public class MainActivity extends Activity {
	public static final String TAG = "AutoAnswerMain";
	private Context context;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		context = this;
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	
	class MyPhoneStateListener extends PhoneStateListener {

		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			// TODO Auto-generated method stub
			switch(state){
			case TelephonyManager.CALL_STATE_IDLE:
				Log.d(TAG, "===>>CALL_STATE_IDLE");
				if(true){
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				break;
				
			case TelephonyManager.CALL_STATE_RINGING:
				Log.d(TAG, "===>>CALL_STATE_RINGING");
				
				/**
				 * Method 1 
				 * Use Reflect
				 */
				/*
				try {
					TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
					
					Class c = Class.forName(telephony.getClass().getName());
					Method m = null;
					m = c.getDeclaredMethod("getITelephony");
					m.setAccessible(true);
					
					//telephonyService = (ITelephony) m.invoke(telephony);
					//telephonyService.answerRingingCall();
					
					
					
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					*/
					
				/**
				 * Method 2
				 * Simulate a press of the headset button to pick up the call
				 */
				/*
				// Simulate a press of the headset button to pick up the call
				// froyo and beyond (Android 2.2以上版本)trigger on buttonUp instead of buttonDown
				public static void answerRingingCall(Context context) { 

				Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG); 
				localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
				localIntent1.putExtra("state", 1); 
				localIntent1.putExtra("microphone", 1); 
				localIntent1.putExtra("name", "Headset"); 
				context.sendOrderedBroadcast(localIntent1, 
				"android.permission.CALL_PRIVILEGED"); 

				Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
				KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN, 
				KeyEvent.KEYCODE_HEADSETHOOK); 
				localIntent2.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent1); 
				context.sendOrderedBroadcast(localIntent2, 
				"android.permission.CALL_PRIVILEGED"); 

				Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
				KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP, 
				KeyEvent.KEYCODE_HEADSETHOOK); 
				localIntent3.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent2); 
				context.sendOrderedBroadcast(localIntent3, 
				"android.permission.CALL_PRIVILEGED"); 

				Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG); 
				localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
				localIntent4.putExtra("state", 0); 
				localIntent4.putExtra("microphone", 1); 
				localIntent4.putExtra("name", "Headset"); 
				context.sendOrderedBroadcast(localIntent4, 
				"android.permission.CALL_PRIVILEGED"); 
				}
				*/
				
				break;
				
			case TelephonyManager.CALL_STATE_OFFHOOK:
				Log.d(TAG, "===>>CALL_STATE_OFFHOOK");
				break;
			}
			
			super.onCallStateChanged(state, incomingNumber);
		}
	}

}
